package webEquipeRechercheGwt.client.allEntityShowingPanel;

import java.util.ArrayList;
import java.util.Collection;

import webEquipeRechercheGwt.client.callback.consultation.CallBackGetAllTeam;
import webEquipeRechercheGwt.client.callback.consultation.commands.CommandGetAllTeamsImplAllTeamPanel;
import webEquipeRechercheGwt.client.entityMirror.TeamMirror;
import webEquipeRechercheGwt.client.handler.navigation.ClickHandlerGetTeamCreationLabel;
import webEquipeRechercheGwt.client.handler.navigation.ClickHandlerTeam;
import webEquipeRechercheGwt.client.services.TeamService;
import webEquipeRechercheGwt.client.services.TeamServiceAsync;
import webEquipeRechercheGwt.client.structure.DataShowingPanel;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;

public class AllTeamPanel extends AllEntityShowingPanel {

	private DataShowingPanel mainPanel;
	private Collection<TeamMirror> teams;
	
	public AllTeamPanel(DataShowingPanel mainPanel) {
		super();
		this.mainPanel = mainPanel;
		teams = new ArrayList<TeamMirror>();
	}

	@Override
	protected void onLoad() {
		super.onLoad();
		updateLabels();
		loadTeamsFromServer();
	}
	
	private void loadTeamsFromServer(){
		TeamServiceAsync service = GWT.create(TeamService.class);
		AsyncCallback<Collection<TeamMirror>> callback = new CallBackGetAllTeam(new CommandGetAllTeamsImplAllTeamPanel(this));
		service.getTeams(callback);
	}

	public void setTeams(Collection<TeamMirror> persons) {
		this.teams = persons;
		updateLabels();
	}
	
	private void addCreationLink(){
		Button b = new Button("create new Team");
		ClickHandler handler = new ClickHandlerGetTeamCreationLabel(mainPanel);
		b.addClickHandler(handler);
		add(b);
	}
	
	private void updateLabels(){
		clear();
		Label title = new Label("List of all Teams");
		add(title);
		for(TeamMirror t : teams){
			Label l = new Label(t.getName());
			ClickHandler clickHandler = new ClickHandlerTeam(t, mainPanel);
			l.addClickHandler(clickHandler);
			add(l);
		}
		addCreationLink();
	}

}
